import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Request,
  Query,
  Put,
} from '@nestjs/common';
import { GoodsService } from './goods.service';

@Controller('goods')
export class GoodsController {
  constructor(private readonly goodsService: GoodsService) {}

  @Post()
  create(@Body() createData, @Request() req) {
    createData.creatorId = req.user.id;
    return this.goodsService.create(createData);
  }

  @Post('list')
  findAll(@Body() createData) {
    console.log('createGoodDto', createData);
    return this.goodsService.findAll({ ...createData });
  }

  @Get(':id')
  findOne(@Param('id') id: number) {
    return this.goodsService.findOne({ id });
  }

  @Put()
  update(@Body() updateData) {
    const {
      goodName,
      goodImg,
      Sku,
      description,
      id,
      goodCode,
      supplierId,
      unitId,
      cateId,
      specialIds,
    } = updateData;
    return this.goodsService.update({
      where: { id: id },
      data: {
        goodName,
        Sku,
        goodImg,
        description,
        goodCode,
        supplierId,
        unitId,
        cateId,
        specialIds,
      },
    });
  }

  @Delete()
  remove(@Query('id') id: number) {
    console.log('id', id);
    return this.goodsService.delete({ id });
  }
}
